package com.zygh.interactiveplatform.module.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.Backups;
import com.zygh.interactiveplatform.module.mapper.BackupsMapper;
import com.zygh.interactiveplatform.util.Backup;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2021-02-01
 */
@Service
public class BackupsService extends ServiceImpl<BackupsMapper, Backups> {
    @Resource
    private BackupsMapper backupsMapper;


    public ResponseDTO addBackups(Backups backups) {
        backups.setStatus("开始备份");
        backups.setCreattime(DateUtil.now());
        int insert = backupsMapper.insert(backups);
        if (insert > 0) {
            boolean backups1 = Backup.backups(backups.getHostip(), backups.getUsername(), backups.getPassword(), backups.getSavepath()
                    , backups.getFilename(), backups.getDatabasename());
            if (backups1) {
                Backups findlast = baseMapper.findlast();
                backups.setId(findlast.getId());
                backups.setStatus("备份成功");
                backupsMapper.updateById(backups);
            }
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.ADD_EXIST);
    }

    public ResponseDTO updateBackups(Backups backups) {
        int insert = backupsMapper.updateById(backups);
        if (insert > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.UPDATE_EXIST);
    }

    public ResponseDTO deleteBackups(Integer id) {
        int insert = backupsMapper.deleteById(id);
        if (insert > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DELETE_EXIST);
    }

    public ResponseDTO findBackupsAll(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<Backups> backupss = backupsMapper.selectList(null);
        if (backupss != null) {
            PageInfo<Backups> pageInfo = new PageInfo<>(backupss);
            return ResponseDTO.succData(pageInfo);
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
    }



}
